/**
 * @class   wosh::services::DesktopNotifierBundle
 * @brief   \ref wosh::interfaces::services::CommunicatorService "Communicator Service"
 *          for desktops (such as Windows, Gnome, KDE). Supports user recognition
 *          (3rd-party authentication) and messaging.
 *
 *
 *
 *
 *
 * @ingroup Services
 ****************************************************************************
 * @version 0.8.151 $Id: DesktopNotifierBundle.h 2597 2010-05-29 00:54:19Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Services_DesktopNotifier_H__
 #define __WOSH_Bundles_Services_DesktopNotifier_H__

 #include <core/BundleGeneric.h>
 #include <core/Containers.h>

 #include <framework/communication/ContactInfo.h>
 #include <framework/communication/TextMessage.h>
 #include <framework/security/SecurityAuthenticationOS.h>

 #include <core/Communicator.h>
 #include <interfaces/Device.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/DesktopNotifier.h>

 #include "IDesktopNotifierListener.h"

class QSystemTrayIcon;
class DesktopNotifierQObject;

using namespace wosh::communication;
namespace wosh {
 namespace services {

 #define _DesktopNotifier_NAME				"DesktopNotifier"
 #define _DesktopNotifier_VERSION			0.54


class DesktopNotifierBundle :	public BundleGeneric,
								public virtual wosh::interfaces::services::CommunicatorService,
								public virtual wosh::interfaces::services::DesktopNotifier,
								public virtual IDesktopNotifierListener
 {
	WOSH_BUNDLE(wosh::services::DesktopNotifierBundle)
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::devices::DesktopNotifier")
	WOSH_BUNDLE_SINGLETON(1,0)


	typedef ListT<Notification*> tNotificationList;


	public:
		DesktopNotifierBundle();

		virtual ~DesktopNotifierBundle();


	public:
		WRESULT bundleStart();
		WRESULT bundleStop();

		void busMessage( const Message& message, const Bus* source );

		bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );

		wosh::interfaces::Device::DEVICE_STATE getDeviceState() const { return this->devState; }

//@}
/** @name IDesktopNotifierListener Interface
 * @{
 ******************************************************************************/
	public:
		void user_MouseNotMoved();
		void user_MouseMoved();
		void user_TrayIconClicked( short mode );
		void user_TrayMessageClicked();

//@}
/** @name DesktopNotifier Interface
 * @{
 ******************************************************************************/
	public:

virtual WRESULT setTrayIcon( QSystemTrayIcon* tray_icon );

WRESULT updateUserLogged( bool online, bool raiseEvent = true );


//@}
/** @name Communicator Interface
 * @{
 ******************************************************************************/
	public:
		bool isUserReachable( const std::string& wosh_username );
		WRESULT sendMessageToUser( const std::string& wosh_username, const std::string& message );
		WRESULT sendMessageTo( const std::string& custom_username, const std::string& message );
		WRESULT notify( const Notification* notification, const Message* notification_message = NULL );

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		std::string getUsernameFromAccountName( const std::string& accountname );

		WRESULT showMessage( const std::string& message );

//@}
	private:
		Response* mmDoForceUser( const Message* requestMessage );
		Response* mmDoSendMessageToUser( const Message* requestMessage );
		Response* mmDoListNotifications( const Message* requestMessage );

	protected:
		BusConnector BusDevices;				///< register myself & work as Device too
		BusConnector BusSecurity;				///< connector to Security bus (posting only)
		ContactInfo account;
		DesktopNotifierQObject* trayObject;

		tNotificationList notifications;
		int notificationsMaxSize;
		int notificationCurrent;

		wosh::interfaces::Device::DEVICE_STATE devState;	///< current state of device

		wosh::security::SecurityTokenProviderOS* credentials;

}; // class def

 }; // namespace services
}; // namespace wosh

#endif //__WOSH_Bundles_Services_DesktopNotifier_H__
